package com.zh.work.controller.platform.trans;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.DateSetUtil;
import com.zh.work.entity.trans.*;
import com.zh.work.service.platform.trans.QrCodeTransErrorService;
import com.zh.work.service.platform.trans.QrCodeTransTodayService;
import com.zh.work.service.platform.trans.QrCodeTransZkService;
import com.zh.work.service.platform.trans.QrCodeTransHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码订单交易控制器
 * @Author: Li.HuiNeng
 * @Create in: 2019-09-09 16:31
 * @Version: 1.0.1
 */
@Api(value = "二维码订单交易控制器", produces = "application/json", tags = {"二维码订单交易控制器"})
@RestController
@RequestMapping("/qrCodeTrans")
public class QrCodeTransController {
    @Autowired
    private QrCodeTransTodayService qrCodeTransTodayService;

    @Autowired
    private QrCodeTransHistoryService qrCodeTransHistoryService;

    @Autowired
    private QrCodeTransZkService qrCodeTransZkService;

    @Autowired
    private QrCodeTransErrorService qrCodeTransErrorService;


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesQrCodeTransToday"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesQrCodeTransToday")
    public JSONResult<List<QrCodeTransTodayEntity>> getPagesQrCodeTransToday(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo")Integer pageSize,
            @ApiParam(value = "二维码当天交易实体类", name = "qrCodeTransTodayEntity") QrCodeTransTodayEntity qrCodeTransTodayEntity) {
        JSONResult<List<QrCodeTransTodayEntity>> jsonResult = new JSONResult<>();
        try {
            Page<QrCodeTransTodayEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeTransTodayEntity> result = this.qrCodeTransTodayService.selectQrCodeTransTodayPages(page, qrCodeTransTodayEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询历史交易记录数据集", tags = {"getPagesQrCodeTransHistory"}, notes = "分页查询历史交易记录数据集")
    @GetMapping("/getPagesQrCodeTransHistory")
    public JSONResult<List<QrCodeTransHistoryEntity>> getPagesQrCodeTransHistory(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo")Integer pageSize,
            @ApiParam(value = "二维码历史交易实体", name = "qrCodeTransHistoryEntity") QrCodeTransHistoryEntity qrCodeTransHistoryEntity) {
        JSONResult<List<QrCodeTransHistoryEntity>> jsonResult = new JSONResult<>();
        try {
            Page<QrCodeTransHistoryEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeTransHistoryEntity> result = this.qrCodeTransHistoryService.selectQrCodeTransHistoryPages(page, qrCodeTransHistoryEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询二维码折扣交易数据集", tags = {"getPagesQrCodeTransZk"}, notes = "分页查询二维码折扣交易数据集")
    @GetMapping("/getPagesQrCodeTransZk")
    public JSONResult<List<QrCodeTransZkEntity>> getPagesQrCodeTransZk(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo")Integer pageSize,
            @ApiParam(value = "二维码折扣交易实体", name = "qrCodeTransZkEntity") QrCodeTransZkEntity qrCodeTransZkEntity) {
        JSONResult<List<QrCodeTransZkEntity>> jsonResult = new JSONResult<>();
        try {
            Page<QrCodeTransZkEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeTransZkEntity> result = this.qrCodeTransZkService.selectQrCodeTransZkPages(page, qrCodeTransZkEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询二维码异常交易数据集", tags = {"getPagesQrCodeTransError"}, notes = "分页查询二维码异常交易数据集")
    @GetMapping("/getPagesQrCodeTransError")
    public JSONResult<List<QrCodeTransErrorEntity>> getPagesQrCodeTransError(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo")Integer pageSize,
            @ApiParam(value = "二维码异常记录实体", name = "qrCodeTransErrorEntity") QrCodeTransErrorEntity qrCodeTransErrorEntity) {
        JSONResult<List<QrCodeTransErrorEntity>> jsonResult = new JSONResult<>();
        try {
            Page<QrCodeTransErrorEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeTransErrorEntity> result = this.qrCodeTransErrorService.selectQrCodeTransErrorPages(page, qrCodeTransErrorEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询二维码交易统计数据", tags = {"getQrCodeTransStatistic"}, notes = "查询二维码订单统计数据")
    @GetMapping("/getQrCodeTransStatistic")
    public JSONResult<List<Map<String, String>>> getQrCodeTransStatistic(@ApiParam(value = "二维码当天交易实体类", name = "qrCodeTransTodayEntity")
                                                                                     QrCodeTransTodayEntity qrCodeTransTodayEntity) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> list = null;
        if (Objects.isNull(qrCodeTransTodayEntity.getStartDateTime())) {
            qrCodeTransTodayEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(qrCodeTransTodayEntity.getEndDateTime())) {
            qrCodeTransTodayEntity.setEndDate(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            list = this.qrCodeTransTodayService.getQrCodeStatistic(qrCodeTransTodayEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (list != null) {
            if (list.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(list);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

    @ApiOperation(value = "二维码充值补发统计数据", tags = {"getQrCodeTransStatistic"}, notes = "二维码充值补发统计数据")
    @GetMapping("/getRechargeStatistic")
    public JSONResult<Map<String, String>> getRechargeStatistic(@ApiParam(value = "二维码当天交易实体类", name = "qrCodeTransTodayEntity")
                                                                            QrCodeTransTodayEntity qrCodeTransTodayEntity) {
        JSONResult<Map<String, String>> jsonResult = new JSONResult<>();
        Map<String, String> map = null;
        if (Objects.isNull(qrCodeTransTodayEntity.getStartDateTime())) {
            qrCodeTransTodayEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(qrCodeTransTodayEntity.getEndDateTime())) {
            qrCodeTransTodayEntity.setEndDate(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            map = this.qrCodeTransTodayService.getRechargeStatistic(
                    qrCodeTransTodayEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (map != null) {
            if (map.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询代理商/机构分润信息", tags = {"getAgentTransList"}, notes = "查询代理商/机构分润信息")
    @GetMapping("/getAgentTransList")
    public JSONResult<List<AgentTransEntity>> getAgentTransList(@ApiParam(value = "商户号", name = "merchNo") @RequestParam("merchNo")String merchNo) {
        JSONResult<List<AgentTransEntity>> jsonResult = new JSONResult<List<AgentTransEntity>>();
        List<AgentTransEntity> list = null;
        try {
            list = qrCodeTransTodayService.getAgentTransList(merchNo);
            jsonResult.setSuccess(true);
            jsonResult.setData(list);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

}
